using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
using Clandestine.Base;

namespace Clandestine.Graphics.Animation
{
	public partial class AnimatedSprite : Sprite
	{
		public int GridWidth { get; private set; }
		public int GridHeight { get; private set; }

		internal Animation CurrentAnimation { get; set; }
        public int CurrentFrame
        {
            get { return CurrentAnimation.CurrentFrame; }
            set { CurrentAnimation.CurrentFrame = value; }
        }

        public bool Playing
        {
            get { return CurrentAnimation.Playing; }
            set 
            {
                if (CurrentAnimation != null)
                    CurrentAnimation.Playing = value; 
            }
        }

        public double Interval
        {
            get { return CurrentAnimation.Interval; }
            set { CurrentAnimation.Interval = value; }
        }

        public int FrameCount
        {
            get { return CurrentAnimation.FrameCount; }
        }
        
        public override Size UnscaledSize
        {
        	get { return new Size(GridWidth, GridHeight); }
        }

        internal override Texture Texture
        {
            get 
            {
                if (CurrentAnimation != null)
                    return CurrentAnimation.Frames[CurrentAnimation.CurrentFrame];
                else
                    return null;
            }
     	}
     	
     	internal AnimatedSprite(RenderList list) : base(list) { }
	
		public AnimatedSprite(RenderList list, string spritesheet, int gridWidth, int gridHeight, SpritesheetSelection[] animations)
			: this(list)
		{
			this.GridHeight = gridHeight;
			this.GridWidth = gridWidth;
			this.AddAnimations(spritesheet, animations);
		}
		
		public AnimatedSprite(RenderList list, string spritesheet, string metadata) : this(list)
		{
			this.AddAnimations(spritesheet, metadata);	
		}
		
		public AnimatedSprite(Graphics graphics, string spritesheet, string metadata) : this(graphics.DefaultRenderList, spritesheet, metadata) { }
		
		public AnimatedSprite(Graphics graphics, string spritesheet, int gridWidth, int gridHeight, SpritesheetSelection[] animations)
			: this(graphics.DefaultRenderList, spritesheet, gridWidth, gridHeight, animations) { }
        
        public void StartAndWait()
        {
            CurrentAnimation.Playing = true;
            CurrentAnimation.FinishedAnimation.WaitOne();
        }

        public void Start(string name = null, double interval = 0)
        {
			if (interval != 0)
				Interval = interval;
			if (name != null)
				SetAnimation(name);

            CurrentAnimation.Playing = true;
        }

        public void Stop()
        {
            CurrentAnimation.Playing = false;
        }
		
		public override void Dispose()
		{	
			// Dispose animations
			foreach (Animation a in this.Animations.Values)
				a.Dispose();
				
			// Set current animation to null, so that texture returns null and base Dispose() does not
			// try and dispose the texture [again]	
			this.CurrentAnimation = null;
			base.Dispose();
		}
	}
}

